package com.ruoyi.web.gd.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.gd.domain.OrderInfo;
import com.ruoyi.web.gd.mapper.OrderInfoMapper;
import com.ruoyi.web.gd.service.IOrderInfoService;
import com.ruoyi.web.gj.domain.WarningInfo;
import com.ruoyi.web.gj.mapper.WarningInfoMapper;
import com.ruoyi.web.lccl.domain.Lccl;
import com.ruoyi.web.lccl.service.YjILcclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 工单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-19
 */
@Service
public class OrderInfoServiceImpl implements IOrderInfoService
{
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysPostService postService;
    @Autowired
    private WarningInfoMapper warningInfoMapper;
    @Autowired
    private YjILcclService yjILcclService;
    /**
     * 查询工单信息
     *
     * @param id 工单信息主键
     * @return 工单信息
     */
    @Override
    public OrderInfo selectOrderInfoById(Long id)
    {
        return orderInfoMapper.selectOrderInfoById(id);
    }

    /**
     * 查询工单信息列表
     *
     * @param orderInfo 工单信息
     * @return 工单信息
     */
    @Override
    public List<OrderInfo> selectOrderInfoList(OrderInfo orderInfo)
    {
        return orderInfoMapper.selectOrderInfoList(orderInfo);
    }

    /**
     * 新增工单信息
     *
     * @param orderInfo 工单信息
     * @return 结果
     */
    @Override
    public int insertOrderInfo(OrderInfo orderInfo)
    {
        SysUser sysUser=new SysUser();
        sysUser.setDeptId(Long.parseLong(orderInfo.getJsdw()));
        List<SysUser> userList=sysUserService.selectUserList(sysUser);
        if(!CollectionUtils.isEmpty(userList)){
            userList.forEach(user ->{
                List<Long> posts=postService.selectPostListByUserId(user.getUserId());
                if (posts.contains(5L)) {
                    orderInfo.setCreateBy(user.getUserName());
                    orderInfo.setStatus("0");
                }
            });
        }
        orderInfo.setCreateTime(DateUtils.getNowDate());
        //把预警状态改成已发布和已派发
        if(orderInfo.getYjid()!=null){
            WarningInfo warningInfo=warningInfoMapper.selectWarningInfoById(orderInfo.getYjid());
            if(warningInfo!=null){
                warningInfo.setClzt("1");
                warningInfo.setFbzt("1");
                warningInfo.setYjczjl("预警无误，请尽快解决！");
                warningInfoMapper.updateWarningInfo(warningInfo);

            }
        }
        //生成处理流程信息
        Lccl lccl=new Lccl();
        lccl.setWorkId(orderInfo.getId().toString());
        lccl.setYjid(orderInfo.getYjid().toString());
        lccl.setCljd("派发");
        lccl.setClrmc(orderInfo.getCreateBy());
        yjILcclService.insertLccl(lccl);

        return orderInfoMapper.insertOrderInfo(orderInfo);
    }

    /**
     * 修改工单信息
     *
     * @param orderInfo 工单信息
     * @return 结果
     */
    @Override
    public int updateOrderInfo(OrderInfo orderInfo)
    {
        orderInfo.setUpdateTime(DateUtils.getNowDate());
        return orderInfoMapper.updateOrderInfo(orderInfo);
    }

    /**
     * 批量删除工单信息
     *
     * @param ids 需要删除的工单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoByIds(Long[] ids)
    {
        return orderInfoMapper.deleteOrderInfoByIds(ids);
    }

    /**
     * 删除工单信息信息
     *
     * @param id 工单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderInfoById(Long id)
    {
        return orderInfoMapper.deleteOrderInfoById(id);
    }

    @Override
    public OrderInfo getWorkOrderById(String id) {
        OrderInfo orderInfo = orderInfoMapper.selectOrderInfoById(Long.valueOf(id));
        if(orderInfo==null){
            return orderInfo;
        }
        List<Map> resultList= new ArrayList<Map>();
        Map map1= new HashMap();
        map1.put("zxhj","预警上报");
        map1.put("zxr","系统(自动)");
        map1.put("kssj",orderInfo.getGjsj());
        map1.put("jssj",orderInfo.getKssj());
        map1.put("tjyj","");
        resultList.add(map1);
        Map map2= new HashMap();
        map2.put("zxhj","工单派单");
        map2.put("zxr",orderInfo.getXdr());
        map2.put("kssj",orderInfo.getKssj());
        map2.put("jssj",orderInfo.getJssj());
        map2.put("tjyj",orderInfo.getYjczjl());
        resultList.add(map2);
        Lccl lccl=new Lccl();
        lccl.setWorkId(id);
        List<Lccl> lcclList=yjILcclService.selectLcclList(lccl);

        if(!CollectionUtils.isEmpty(lcclList)) {
            for (int i = 0; i < lcclList.size(); i++) {
                Map map = new HashMap();
                Lccl lccl1 = lcclList.get(i);
                if(i==lcclList.size()-1) {
                    if(orderInfo.getClzt().equals("2")){
                        map.put("zxhj", "工单处理");
                        map.put("zxr", lccl1.getClrmc());
                        map.put("kssj", lccl1.getCreateTime());
                        map.put("jssj", orderInfo.getUpdateTime());
                        map.put("tjyj", orderInfo.getCzxx());
                        resultList.add(map);
                    }else{
                        map.put("zxhj", "转发");
                        map.put("zxr", lccl1.getClrmc());
                        map.put("kssj", lccl1.getCreateTime());
                        map.put("jssj", lccl1.getCreateTime());
                        map.put("tjyj", lccl1.getClyj());
                        resultList.add(map);
                    }
                }else{
                    map.put("zxhj", "转发");
                    map.put("zxr", lccl1.getClrmc());
                    map.put("kssj", lccl1.getCreateTime());
                    map.put("jssj", lccl1.getUpdateTime());
                    map.put("tjyj", lccl1.getClyj());
                    resultList.add(map);
                }
            }
        }
        orderInfo.setCzjl(resultList);

        return orderInfo;
    }
}
